<?php
/**
* Copyright 2009 Waag Society <http://www.waag.org>
* This file is part of Annotrax.
*
* Annotrax is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* Annotrax is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Annotrax.
* If not, see http://www.gnu.org/licenses/gpl.html
*/

/**
 * Action Helper for loading the full user object.
 *
 *
 * @uses Zend_Controller_Action_Helper_Abstract
 */
class At_Helper_User extends Zend_Controller_Action_Helper_Abstract
{
    /**
     * @var Zend_Loader_PluginLoader
     */
    public $pluginLoader;

	/**
	 * @var Zend_Session_Namespace
	 */
	private $_session;

    /**
     * Constructor: initialize plugin loader
     * @return void
     */
    public function __construct(){
        $this->pluginLoader = new Zend_Loader_PluginLoader();
    }

    /**
     * Strategy pattern: call helper as broker method
     *
     * @return Default_Model_User $user
     */
    public function direct(){
		return $this->getUser();
    }

	/**
	 * Retrieve a user from the session cache to offload DB usage
	 *
	 * @retrun Default_Model_User $user
	 */
	public function getUser(){
		if( null === $this->getSession()->user ){
			$this->getSession()->user = new Default_Model_User();
		}

		return $this->getSession()->user;
	}

	/**
	 * Instantiate a Zend_Session_Namespace object for 'user'
	 *
	 * @return Zend_Session_Namespace $session
	 */
	private function getSession(){
		if( null === $this->_session ){
			$this->_session = new Zend_Session_Namespace( 'user' );
		}

		return $this->_session;
	}
}
